package com.zy.je.common.core.utils.macroparam;

import com.zy.je.common.core.utils.lang.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author zy
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MacroParam {
    private String content;
    private List<MacroParamData> paramList;

    public String replace() {
        if (null == paramList || paramList.size() <= 0) {
            return content;
        }
        final String[] process = {Optional.ofNullable(content).orElse("")};
        paramList.forEach(it -> {
            String v = ObjectUtil.toString(it.getValue());
            process[0] = process[0].replace(it.getParamName(), v);
            //支持大写宏参数
            process[0] = process[0].replace(it.getParamName().toUpperCase(), v);
        });
        return process[0];
    }

    public static MacroParamBuilder builder() {
        return new MacroParamBuilder();
    }

    public static class MacroParamBuilder {
        private String content;
        private List<MacroParamData> paramList;

        MacroParamBuilder() {
        }

        public MacroParamBuilder content(final String content) {
            this.content = content;
            return this;
        }

        public MacroParamBuilder addParam(final String paramName, final String value) {
            if (null == this.paramList) {
                this.paramList = new ArrayList<>();
            }
            this.paramList.add(MacroParamData.build(paramName, value));
            return this;
        }

        public MacroParam build() {
            return new MacroParam(this.content, this.paramList);
        }

        @Override
        public String toString() {
            return "MacroParam.MacroParamBuilder(srcContent=" + this.content + ", paramList=" + this.paramList + ")";
        }
    }

}
